const assert = require('assert');
const BasicController = require('../BasicController.js');
const fs = require('fs');
const path = require('path');
//  通过promisify方法把rename方法封装成Promise接口
const rename = think.promisify(fs.rename, fs);

module.exports = class extends BasicController {
  constructor(ctx) {
    super(ctx);
    assert(think.isFunction(this.model), 'this.model must be a function');
    this.MODEL = this.ctx.state.ApiInfo.model;
    this.params = {
      model: this.MODEL,
      data: this.post()
    };
  }
  // 修改学生数据
  async updatestuAction() {
    const params = this.params;
    const getRow = await this.model(params.model).updatestuAction(params);
    let affectedRows;
    if (!think.isEmpty(getRow)) {
      affectedRows = await this.model(params.model).where(params.data.stu_id).update(params.data);
      return this.success(affectedRows);
    } else {
      const data = await this.model(params.model).addAction(params);
      return this.success(data);
    }
  };
  // 上传图片
  async uploadImgAction() {
    try {
      const file = this.file();
      const imgKeys = Object.getOwnPropertyNames(file);
      for (const i of imgKeys) {
        let imgs = [];
        if (think.isObject(file[i])) {
          imgs.push(file[i]);
        } else if (think.isArray(file[i])) {
          imgs = [...file[i]];
        }
        for (const item of imgs) {
          const filepath = path.join(think.ROOT_PATH, `www/static/image/${item.name}`);
          think.mkdir(path.dirname(filepath));
          await rename(item.path, filepath);
        }
      }
      this.success('上传成功');
    } catch (error) {
      this.fail(1, '上传失败');
    }
  };

  async downloadimgAction() {
    const {
      // filename,
      filepath
    } = this.post();
    // const userAgent = this.userAgent().toLowerCase();
    // const userAgent = this.userAgent().toLowerCase();
    // console.log('==================================')
    // console.log(userAgent);
    // let hfilename = '';
    // if (userAgent.indexOf('mise') >= 0 || userAgent.indexOf('chrome') >= 0) {
    //   hfilename = `=${encodeURIComponent(filename)}`;
    // } else if (userAgent.indexOf('firefox') >= 0) {
    //   hfilename = `*="utf8''${encodeURIComponent(filename)}`;
    // } else {
    //   hfilename = `=${Buffer.from(filename).toString('binary')}`;
    // }
    // this.ctx.set('Content-Disposition', `attachment; filename${hfilename}`);
    this.ctx.download(filepath);
  }
};

/**
 * @api {POST} /student_info/student_info/listall /listall
 * @apiDescription 获取学生信息列表
 * @apiGroup student_info
 * @apiParam {Number} [stu_id] 用户ID
 * @apiParam {String} [stu_num] 学号
 * @apiParam {Number} [role_id] 角色ID
 * @apiParam {Number} [group_id] 组别id
 * @apiParam {String} [stu_name] 名字
 * @apiParam {String} [stu_major] 专业
 * @apiParam {String} [stu_class] 班级
 * @apiParam {Number} [stu_teacher_id] 负责教师id
 * @apiParam {String} [stu_level] 级别
 * @apiVersion 1.0.0
 * @apiErrorExample {json} 错误返回值:
 *     {
 *        "code": ,
 *        "msg": "获取失败",
 *        "error": {
 *        },
 *     }
 *
 * @apiSuccessExample {json} 正确返回值:
 *     {
 *  "errno": 0,
 *  "errmsg": "",
 *   "data": [
 *       {
 *           "stu_id": 1,
 *           "stu_num": 5120152620,
 *           "group_id": null,
 *           "stu_name": "这是一个测试名字"",
 *           "Stu_Grade": "2019",
 *           "stu_major": null,
  *          "stu_class": null,
  *          "Stu_Tel": 18281548157,
  *          "stu_idcard": null,
  *          "stu_bankcard": null,
  *          "Stu_Photo": null,
  *          "stu_email": null,
  *          "stu_teacher_id": null,
  *          "stu_level": null,
  *          "create_time": "2018-07-17 14:52:37",
  *          "update_time": "2018-07-17 14:52:37",
  *          "role_id": ""
 *    }
 *   ],
 *   }
 */

/**
 * @api {get} /student_info/student_info/get /get
 * @apiDescription 根据id查询单个学生信息
 * @apiGroup student_info
 * @apiParam {String} [stu_id] 用户ID
 *
 *
 * @apiParamExample {string} 请求参数格式:
 *    ?id=1
 *
 * @apiVersion 1.0.0
 * @apiErrorExample {json} 错误返回值:
 *     {
 *        "code": 500,
 *        "msg": "",
 *        "error": {
 *        },
 *     }
 * @apiSuccessExample {json} 正确返回值:
*{
*    "errno": 0,
*    "errmsg": "",
*    "data": {
*        "id": 1,
*        "stu_num": 5120152620,
*        "group_id": null,
*        "stu_name": "这是一个测试名字",
*        "Stu_Grade": "2019",
*        "stu_major": null,
*        "stu_class": null,
*        "Stu_Tel": 18281548157,
*        "stu_idcard": null,
*        "stu_bankcard": null,
*        "stu_email": null,
*        "stu_teacher_id": null,
*        "stu_level": null,
*        "create_time": "2018-07-17 14:52:37",
*        "update_time": "2018-07-17 14:52:37",
*        "role_id": ""
*    }
*}
 */

/**
 * @api {delete} /student_info/student_info/del /del
 * @apiDescription 根据id删除学生信息
 * @apiGroup student_info
 *
 * @apiParam {String} stu_id 用户ID
 *
 *
 * @apiParamExample {object} 请求参数格式:
 *  {
 * stu_id: 2,
 * stu_id: 4
 * }
 *
 * @apiVersion 1.0.0
 * @apiErrorExample {json} 错误返回值:
 *     {
 *        "code": 500,
 *        "msg": "",
 *        "error": {
 *        },
 *     }
 * @apiSuccessExample {json} 正确返回值:
 *{
 *  "errno": 0,
 *   "errmsg": "",
 *   "data": 2
 *}
*}
 */

/**
 * @api {post} /student_info/student_info/updatestu /updatestu
 * @apiDescription 根据id查询修改学生信息
 * @apiGroup student_info
 *
 * @apiParam {String} stu_id 用户ID
 * @apiParam {String} stu_num 学号
 * @apiParam {String} role_id 角色ID
 * @apiParam {String} group_id 组别id
 * @apiParam {String} stu_name 名字
 * @apiParam {String} stu_major 专业
 * @apiParam {String} stu_class 班级
 * @apiParam {String} [stu_idcard] 身份证号码
 * @apiParam {String} Stu_Tel 电话号码
 * @apiParam {String} [stu_bankcard] 银行账户
 * @apiParam {String} stu_email 电子邮箱
 * @apiParam {String} stu_teacher_id 负责教师id
 * @apiParam {String} stu_level 级别
 *
 * @apiParamExample {object} 请求参数格式:
 *  {
 *  stu_class: '2015',
 *  id: 6,
 *  Stu_Tel: 18281575555
 * }
 *
 * @apiVersion 1.0.0
 * @apiErrorExample {json} 错误返回值:
 *     {
 *        "code": 500,
 *        "msg": "",
 *        "error": {
 *        },
 *     }
 * @apiSuccessExample {json} 正确返回值:
*{
 *    "errno": 0,
 *   "errmsg": "",
 *   "data": 1
*}
*/
